<?php
//员工
namespace app\crm\controller;

use weworkapi\api\datastructure\user\User;
use think\Db;
use app\crm\model\Sys;


class Sample extends \SampleController
{
    public function subscribe($staff_id){

       //判断是否有员工数据
       // Db::name('crm_ceshi')->insert(['var'=>$staff_id]);
       $data =  Db::name('crm_staff')->where('staff_id',$staff_id)->find();
       if(!empty($data)){//有数据
//           $a = $this->qiyeweixin->UserGet2($staff_id);
//            if(empty($a->userid)){
//                $did = $this->synchronization();
//                $user = new User();
//                {
//                    $user->userid = $data['staff_id'];
//                    $user->name = $data['staff_name'];//名称
//                    $user->mobile = $data['phone'];//手机
//                    $user->department = array($did);//部门
//                    $user->position = $data['duty'];//职位
//                    $user->gender = $data['sex'];//性别
//                }
//                $this->qiyeweixin->UserCreate($user);
//
//           }
           Db::name('crm_staff')->where('staff_id',$staff_id)->update(['attention_type'=>1]);
       }

    }
    public function Nosubscribe($staff_id){

        //判断是否有员工数据
      //  Db::name('crm_ceshi')->insert(['var'=>$staff_id]);
        $data =  Db::name('crm_staff')->where('staff_id',$staff_id)->find();
        if(!empty($data)){//有数据
//            $a = $this->qiyeweixin->UserGet2($staff_id);
//            if(empty($a->userid)){
//                $did = $this->synchronization();
//                $user = new User();
//                {
//                    $user->userid = $data['staff_id'];
//                    $user->name = $data['staff_name'];//名称
//                    $user->mobile = $data['phone'];//手机
//                    $user->department = array($did);//部门
//                    $user->position = $data['duty'];//职位
//                    $user->gender = $data['sex'];//性别
//                }
//                $this->qiyeweixin->UserCreate($user);
                Db::name('crm_staff')->where('staff_id',$staff_id)->update(['attention_type'=>0]);
          //  }
        }

    }
    /**
     *微信同步
     */
    public function synchronization(){
        //获取微信所有本门
        $DepartmentList = $this->qiyeweixin->DepartmentList();
        $is_xx = 0;
        $did = 0;
        foreach ($DepartmentList as $list2){
            if($list2->name=='上下游互动'){
                $is_xx++;
                $did = $list2->id;
            }
        }
        $departmentData =  new \weworkapi\api\datastructure\Department();
        if($did==0){
            $departmentData->parentid = 1;
            $departmentData->name = '上下游互动';
            $this->qiyeweixin->DepartmentCreate($departmentData);
        }
        foreach ($DepartmentList as $list3){
            if($list3->name=='上下游互动'){

                $did = $list3->id;
            }
        }
        $DepartmentList = $this->qiyeweixin->DepartmentList();
        $SYS = new Sys();
        $SYS->where('id',1)->update(['crm_sxyhd_did'=>$did]);

        $addDatas = [];
        $Department = new \app\crm\model\Department();
        $DepartmentDb = $Department->DepartmentDb();
        $pcDepartment = $DepartmentDb->column('department_id,parent_id,department_name','department_id');
        //修改
        foreach ($DepartmentList as $k=>$v){
            if (array_key_exists($v->id,$pcDepartment))
            {
                $up['department_name'] = $v->name;
                $up['parent_id'] = $v->parentid;
                $Department->where('department_id',$v->id)->update($up);
                unset($pcDepartment[$v->id]);
            }
            else
            {
                $addDatas[] = $v;
            }

        }

        //pc新增
        if(count($addDatas)>0){

            foreach ($addDatas as $k1=>$v1){
                if($v1->name!='上下游互动'){
                    $add['department_name'] = $v1->name;
                    $add['parent_id'] = $v1->parentid;
                    $add['department_id'] = $v1->id;
                    $Department->insert($add);
                }else{

                    //判断是否有上下游互动部门
                    $is_xx++;
                }
            }
        }
        //wx新增
        if(count($pcDepartment)>0){
            $departmentData =  new \weworkapi\api\datastructure\Department();
            foreach ($pcDepartment as $k2=>$v2){
                {
                    $departmentData->id = $v2['department_id'];
                    $departmentData->name =  $v2['department_name'];
                    $departmentData->parentid =  $v2['parent_id'];
                }
                $this->qiyeweixin->DepartmentCreate($departmentData);
            }

        }

    return $did;
    }




}
